﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using System.IO;
using COFE.Core.Entry;

namespace COFE.Core.Lister
{
    /// <summary>
    /// This is for use in test only.
    /// </summary>
    public class DummyDirectoryLister : PrimaryDirectoryListerBase<string>
    {
        public DummyDirectoryLister()
            : base(null, "DUMMY")
        {
            _list = (p) => { return new List<string>(new string[] { (p + "\\sub"), (p + "file.txt") }); };
            _isDirectory = (p) => { return !p.ToLower().EndsWith(".txt"); };
            _getParent = (p) =>
            {
                string parentPath = COFEPath.GetDirectoryName(p);
                return String.IsNullOrEmpty(parentPath) ? null : parentPath;
            };


            //Primar Directory Lister
            _parse = (p) => { return p; };
            _getRootDirectory = () => { return new string[] { "" };  };
        }

        public override IDirectoryController CreateController()
        {
            return new DummyDirectoryController(this);
        }

        public override IStreamProvider CreateStreamProvider()
        {
            return new DummyStreamProvider(this);
        }

        public override IPropertyProvider CreatePropertyProvider(string item)
        {
            return new DummyPropertyProvider(ListerKey, item);
        }
    }

    public class DummyPropertyProvider : PropertyProviderBase<string>
    {
        public DummyPropertyProvider(string listerKey, string path)
            : base(path)
        {
            base.RegisterMethod(FileSystemInfoProperties.FullParseName, (e,p) => { return COFEPath.FormatFullParseName(listerKey, path); });
            base.RegisterMethod(FileSystemInfoProperties.Label, (e,p) => { return path; });
            base.RegisterMethod(FileSystemInfoProperties.FileAttributes, (e, p) => { return FileAttributes.Normal; });
            base.RegisterMethod(FileSystemInfoProperties.COFEAttributes, (e, p) => { return COFEAttributes.None; });

            base.RegisterMethod(FileSystemInfoProperties.IsExists, (e,p) => { return true; });
            base.RegisterMethod(FileSystemInfoProperties.CreationTimeUtc,
               (e,p) => { return DateTime.UtcNow; });
            base.RegisterMethod(FileSystemInfoProperties.LastWriteTimeUtc,
               (e, p) => { return DateTime.UtcNow; });

            base.RegisterMethod(IconProperties.SmallIcon,
              (e, p) => { return new System.Drawing.Bitmap(16, 16); });
        }

        
    }

    public class DummyDirectoryController : DirectoryControllerBase<string>
    {
        public DummyDirectoryController(DummyDirectoryLister lister)
            : base(lister)
        {

        }
    }

    public class DummyStreamProvider : StreamProviderBase<string>
    {
        public DummyStreamProvider(DummyDirectoryLister lister)            
            : base(lister)
        {
            _getStream = (file, mode, access, share) =>
            {
                var ms = new MemoryStream();
                StreamWriter sr = new StreamWriter(ms);
                sr.WriteLine("dummy");
                sr.Flush();
                return ms;
            };
        }

    }
}
